﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CDStore3Tiers;
namespace CDStore3Tiers
{
    public class XLSuKien
    {
        StoreCDDataContext db = new StoreCDDataContext();
        /// <summary>
        /// Cập nhật sự kiện
        /// </summary>
        /// <param name="dgv">datagridview kết quả</param>
        public void updateSuKien(DataGridView dgv)
        {
            if (dgv.Rows.Count > 0)
            {
                XuLyForm XL = new XuLyForm();
                dgv.EndEdit();
                foreach (DataGridViewRow r in dgv.Rows)
                {
                    int maSK = int.Parse(r.Cells["cnameMaSK"].Value.ToString());
                    string tenSK = r.Cells["cnameTenSK"].Value.ToString().Trim();
                    string noiDung = r.Cells["cnameND"].Value.ToString().Trim();
                    int giamGia = int.Parse(r.Cells["cnameGG"].Value.ToString().Trim());
                    DateTime ngayBD = DateTime.Parse(r.Cells["cnameNgayBD"].Value.ToString());
                    DateTime ngayKT = DateTime.Parse(r.Cells["cnameNgayKT"].Value.ToString());

                    SuKien SK = db.SuKiens.Single(t => t.MaSK.Equals(maSK));
                    SK.TenSK = tenSK;
                    SK.NoiDung = noiDung;
                    SK.GiamGia = giamGia;
                    SK.NgayBD = ngayBD;
                    SK.NgayKT = ngayKT;
                    db.SubmitChanges();
                }
            }
        }

        public List<SuKien> searchByID(List<SuKien> SK, string val)
        {
            val = val.ToLower();
            return SK.Where(c => c.MaSK.ToString().Contains(val) || c.TenSK.ToString().ToLower().Contains(val) || c.NoiDung.ToString().ToLower().Contains(val)).ToList<SuKien>();
            
        }

        public List<SuKien> getBySoluong(int index)
        {
            db = new StoreCDDataContext();
            List<SuKien> listduoi15 = new List<SuKien>();
            List<SuKien> listduoi30 = new List<SuKien>();
            List<SuKien> listtren30 = new List<SuKien>();
            List<SuKien> list = new List<SuKien>();

            foreach (SuKien sk in db.SuKiens)
            {
                if (sk.GiamGia <= 15) listduoi15.Add(sk);
                else if (sk.GiamGia <=30 ) listduoi30.Add(sk);
                else listtren30.Add(sk);
            }
            switch (index)
            {
                case 0:
                    list = db.SuKiens.ToList<SuKien>();
                    break;
                case 1:
                    list = listduoi15;
                    break;
                case 2:
                    list = listduoi30;
                    break;
                case 3:
                    list = listtren30;
                    break;
            }
            return list;
        }


        public int KiemTraThoiGian(DateTime ngay)
        {
            int idSukien = 0; 
            var tg = db.SuKien_SelectByDate(ngay).FirstOrDefault();
            bool kt = (tg == null);
            if (!kt)
            {
                idSukien = tg.MaSK;
            }       
            return idSukien;
        }

        public bool khongtrunglap(int mask, int madia)
        {
            var gg = db.DSGiamGia_SelectByID(mask, madia);
            return (gg.FirstOrDefault() == null);
        }

        public List<vwDSGiamGia> ShowDSGG(int IDSuKien)
        {
            List<vwDSGiamGia> list = (from ds in db.vwDSGiamGias
                                      where ds.MaSK == IDSuKien
                                      select ds).ToList<vwDSGiamGia>();
            return list;
        }

        public DateTime NgayKT(int IDSK)
        {
            var kt = db.SuKien_SelectByID(IDSK).FirstOrDefault();
            return kt.NgayKT;
        }
        public int PhanTramGG(int IDSK)
        {
            var phantram = db.SuKien_SelectByID(IDSK).FirstOrDefault();
            return phantram.GiamGia;
        }
    }
}
